package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/loginUser")
public class LoginUserApiController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysPasswordService sysPasswordService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public AjaxResult login(String username, String password) {
        List<Map<String,Object>> user = sysUserService.selelctByUserName(username);
        if (user != null) {
            String userPassword = user.get(0).get("password").toString();
            String encryptPassword = sysPasswordService.encryptPassword(username, password, user.get(0).get("salt").toString());
            if (userPassword.equals(encryptPassword)){
                return AjaxResult.success(user.get(0));
            }else{
                return AjaxResult.error("密码错误");
            }
        }else{
            return AjaxResult.error("用户不存在");
        }
    }
}
